package com.ciwong.epaper.modules.viedoexplantion.imp;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.viedoexplantion.LoadUi;

/**
 * Created by Young  on 2016/7/5.
 * 完型填空题加载UI布局类
 */
public class BlankLoadUi implements LoadUi{
    private TextView tv_close_blank_content;

    //    private TextView mTextView = null;
    private SpannableString msp = null;
    private  String[ ] mTextStrArr;
    private  String[ ] mAnsStrArr;
    private  int mTextCursor=0;






    @Override
    public View loadLayout(Object  o, Context context,Boolean b) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_close_blank,null);
        tv_close_blank_content = (TextView) view.findViewById(R.id.tv_close_blank_content);
        initData( );
        disTextWithOutAns( );
        return view;
    }




    private void initData() {
        mTextStrArr=getTextStrArr();
        mAnsStrArr=getAnsStrArr();
    }


    private void  disTextWithOutAns( )
    {
        String text = matchStrWithoutAns(mTextStrArr, mAnsStrArr);
        //创建一个 SpannableString对象
//        msp = new SpannableString("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信下划线实验     ask       X轴综合/bot");
        msp = new SpannableString(text);
        mTextCursor=0;
        for(int i=0;i<mTextStrArr.length;i++) {
            mTextCursor=mTextCursor+mTextStrArr[i].length();
            //先对2求模式为了防止是单数偶数造成问题
            int offset=(mAnsStrArr[i].length()/2)*2+7;
            msp.setSpan(new UnderlineSpan( ), mTextCursor, mTextCursor+offset, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            msp.setSpan(new ForegroundColorSpan(Color.LTGRAY), mTextCursor, mTextCursor+offset, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTextCursor=mTextCursor+offset;
        }
        tv_close_blank_content.setText(msp);
        tv_close_blank_content.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void  disTextWithAns( )
    {
        String text = matchStrWithAns(mTextStrArr, mAnsStrArr);
        //创建一个 SpannableString对象
//        msp = new SpannableString("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信下划线实验     ask       X轴综合/bot");
        msp = new SpannableString(text);
        mTextCursor=0;
        for(int i=0;i<mTextStrArr.length;i++) {
            mTextCursor=mTextCursor+mTextStrArr[i].length();
            //先对2求模式为了防止是单数偶数造成问题
            int offset=(mAnsStrArr[i].length())+6;
            msp.setSpan(new UnderlineSpan( ), mTextCursor, mTextCursor+offset, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            msp.setSpan(new ForegroundColorSpan(Color.GREEN), mTextCursor, mTextCursor+offset, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTextCursor=mTextCursor+offset;
        }
        tv_close_blank_content.setText(msp);
        tv_close_blank_content.setMovementMethod(LinkMovementMethod.getInstance());
    }



    private String[] getTextStrArr( )
    {
        int size=10;
        String[] strArr = new String[size];
        for(int i=0;i<size;i++) {
            switch (i) {
                case 0:
                    strArr[i]="  It was the afternoon of April 1. The bell rang";
                    break;
                case 1:
                    strArr[i]="the end of school. The students ";
                    break;
                case 2:
                    strArr[i]="their books into their bags. They were ready";
                    break;
                case 3:
                    strArr[i]="home Just at the moment, their English teacher came into the classroom and said to them, “Hello, boys and girls. Now go over the English lessons, and we'll have a test ";
                    break;
                case 4:
                    strArr[i]="half an hour.";
                    break;
                case 5:
                    strArr[i]="these words, she left.";
                    break;
                case 6:
                    strArr[i]="The students looked at each other in surprise. How sudden! The teacher had never told them they would have a test, yet they took out their English books and began to work hard.\n" +
                            "They studied and studied. It was getting dark,";
                    break;
                case 7:
                    strArr[i]="the teacher hadn’t come yet! The monitor went out to the teachers’ office";
                    break;
                case 8:
                    strArr[i]="minutes later, the English teacher came in with a smile. But she had ";
                    break;
                case 9:
                    strArr[i]="in her hand!";
                    break;
            }
        }

        return  strArr;
    }

    private String[] getAnsStrArr()
    {
        int size=10;
        String[] strArr = new String[size];
        for(int i=0;i<size;i++) {
            switch (i) {
                case 0:
                    strArr[i]="for";
                    break;
                case 1:
                    strArr[i]="would put";
                    break;
                case 2:
                    strArr[i]="going";
                    break;
                case 3:
                    strArr[i]="after";
                    break;
                case 4:
                    strArr[i]="Without";
                    break;
                case 5:
                    strArr[i]="few";
                    break;
                case 6:
                    strArr[i]="everything";
                    break;
                case 7:
                    strArr[i]="what’s the date";
                    break;
                case 8:
                    strArr[i]="laughed";
                    break;
                case 9:
                    strArr[i]="date";
                    break;
            }
        }

        return  strArr;
    }


    private String matchStrWithoutAns(String[]  textStrArr,String[]  ansStrArr) {
//        String textWithoutAns=null;
        StringBuilder sb = new StringBuilder();
        if(null != textStrArr && textStrArr.length>0&&null != ansStrArr && ansStrArr.length>0)
        {
            for(int i=0;i<textStrArr.length;i++) {
                sb.append(textStrArr[i]);
                int spacesNumber = ansStrArr[i].length() / 2;
                //加3是为了填答案下划线长于答案字母
                spacesNumber=spacesNumber+3;
                for(int j=0;j<spacesNumber;j++) {
                    sb.append(" ");
                }
                sb.append(String.valueOf(i));
                for(int j=0;j<spacesNumber;j++) {
                    sb.append(" ");
                }

                if (ansStrArr[i].length() % 2 == 1) {
                }
            }
        }

        Log.d("stringcom", "sb.toString()"+sb.toString());

        return  sb.toString();
    }



    private String matchStrWithAns(String[]  textStrArr,String[]  ansStrArr) {
//        String textWithoutAns=null;
        StringBuilder sb = new StringBuilder();
        if(null != textStrArr && textStrArr.length>0&&null != ansStrArr && ansStrArr.length>0)
        {
            for(int i=0;i<textStrArr.length;i++) {
                sb.append(textStrArr[i]);
                int spacesNumber = ansStrArr[i].length() / 2;
                //加3是为了填答案下划线长于答案字母
                spacesNumber=3;
                for(int j=0;j<spacesNumber;j++) {
                    sb.append(" ");
                }
                sb.append(ansStrArr[i]);

                for(int j=0;j<spacesNumber;j++) {
                    sb.append(" ");
                }
            }
        }
        Log.d("stringcom", "sb.toString()"+sb.toString());
        return  sb.toString();
    }
}
